<?php

namespace App\Admin\Controllers;

use App\Models\SArticleInfo;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ArticleController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('文章')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('文章')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('文章')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('文章')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SArticleInfo);

        $grid->id('ID')->sortable();
        $grid->title('标题')->limit(30);
        $grid->tags('标签')->limit(30);
        $grid->desc('简介')->limit(30);
        $grid->poster('封面')->image();
        // $grid->content('内容')->limit(30);
        $grid->created_at('创建时间')->sortable();
        $grid->updated_at('更新时间');

        //禁用导出
        $grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SArticleInfo::findOrFail($id));

        $show->id('ID');
        $show->title('标题');
        $show->tags('标签');
        $show->desc('简介');
        $show->poster('封面')->image();
        $show->content('内容')->unescape()->as(function ($content) {
            return "{$content}";
        });
        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SArticleInfo);


        $form->display('id', 'ID');

        $form->text('title','标题')->rules("required");
        $form->text('tags','标签')->help("多个标签用英文逗号隔开")->rules("required");
        $form->text('desc','简介')->rules("required");

        $form->image('poster','封面')->rules("required");
        
        $form->editor('content','内容')->rules("required");
        
        $form->hidden('admin_id')->value( \Admin::user()->id );

        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');

        
        $form->footer(function ($footer) {

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
